<?php
/**
 * 问卷调查
 * 张名尊 20140511
 */ 
class questionAction extends apiAction{
    
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('question');
    }
    
    public function answer(){
        
        $qid = $this->_request('qid','intval',0);
        if(!empty($qid)){
            $list = $this->_mod->getQuestion($qid);
            $info = M('question')->where(array('id'=>$qid))->find();
            
            $this->assign('info',$info);
            $this->assign('list',$list);
            $this->assign('qid',$qid);
        }
        $this->display();
        
    }
    public function questionList(){

        $list = M('question')->where(array('status'=>1,'pid'=>0))->select();
        $this->assign('list',$list);    
        $this->display();
        
    }
    
    /**
     * 提交答案接口
     */ 
    public function actAnswer(){
       // print_r($_REQUEST);
        $qid = $this->_request('questionId','intval',0);
        $data = array();
        foreach($_REQUEST['v'] as $k=>$v){
            $temp['questionId'] = $qid;  //问卷
            $temp['answerValue'] = is_array($v)?implode(',',$v):$v; //内容
            $temp['titleId'] = $k; // 题目 
            $temp['userId'] = getUserId();
            $temp['createTime'] = time();
            $data[] = $temp;
        }
        $res = D('answer')->addAll($data,array(),1);
        if($res){
            $this->ajaxReturn(200, L('success'));
        }else{
            $this->ajaxReturn(300, L('error'));
        }
        
    }
    
    public function detail(){
        $qid = $this->_request('qid','intval',0);;
        $userId  = getUserId();
        if(!empty($qid)){
            $list = $this->_mod->getQuestion($qid);
            $this->assign('list',$list);
            $valueList = M('answer')->where(array('userId'=>$userId,'questionId'=>$qid))->select();
            foreach($valueList as $k=>$v){
                $temp[$v['titleId']] = $list['question'][$v['titleId']]['type']==4?explode(',',$v['answerValue']):$v['answerValue'];
            }
            $this->assign('valueList',$temp);
            $this->assign('qid',$qid);
        }
        $this->display();
        
    }
    
    
    
}


?>